﻿using Common.Contract.Authentication.Record;
using Common.Contract.CrossSelling.Record;
using Common.Contract.CrossSelling.Request;
using Common.Contract.CrossSelling.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveCustomerPreApprovedBilServiceCommand : ProviderServiceCommand<IProviderFactory, RetrievePreApprovedCustBilRequest, RetrievePreApprovedCustBilResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveCustomerPreApprovedBilServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrievePreApprovedCustBilRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
                TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrievePreApprovedCustBilResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<XSellingCustomerRecord> records = new List<XSellingCustomerRecord>();
                List<PreApprovedBilNew> listCustomer = new List<PreApprovedBilNew>();
                listCustomer = Providers.CrossSellingProducts.RetrieveListCustPreApprovedBil(out totalRecord, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new XSellingCustomerRecord
                    {
                        Zone = x.Region,
                        DaoName = x.DaoName == null ? string.Empty : x.DaoName,
                        Dao = x.Dao == null ? string.Empty : x.Dao,
                        Branch = x.BranchNameSme,
                        CIF = x.Cif,
                        Name = x.CusName,
                        VpbIndustry = Request.Lang.Equals("vi") ? x.IndustryName : x.IndustryNameEn,
                        DateOpenCif = x.CusOpenDate.HasValue ? x.CusOpenDate.Value : DateTime.MinValue,
                        DateAprove = x.DateApprove.HasValue ? x.DateApprove.Value : DateTime.MinValue,
                        SumLimit = x.SumLimit.ToString("#,###", cul.NumberFormat),
                        TimeRemain = x.TimeRemain.ToString(),
                        LimitBilDuocCap = x.LimitBillDuocCap.ToString("#,###", cul.NumberFormat),
                    }).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }

    }
}
